package com.qianfeng.zhangwei.xutils.db.dao.impl;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.qianfeng.zhangwei.xutils.bean.User;
import com.qianfeng.zhangwei.xutils.db.DbHelper;
import com.qianfeng.zhangwei.xutils.db.UserSqliteOpenHelper;
import com.qianfeng.zhangwei.xutils.db.dao.UserDao;

import java.util.ArrayList;
import java.util.List;

/**
 * @说 明：
 * @项目名称：Sz1504Adv
 * @包 名： com.qianfeng.zhangwei.xutils.db.dao.impl
 * @类 名： UserDaoImpl
 * @创 建人： zhangwei
 * @创建时间：2015-08-04 21:52
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注： data    object
 *
 * 操作数据的方式
 *
 *
 * Sql 语句
 * Database 自带方法
 */
public class UserDaoImpl implements UserDao {
    private UserSqliteOpenHelper helper;

    public UserDaoImpl() {
        helper = new UserSqliteOpenHelper();
    }

    @Override
    public void save(User user) {
        //以写的形式打开数据   当磁盘空间已满, 向外抛异常
//        SQLiteDatabase database = helper.getWritableDatabase();
        //以读写的形式打开数据
        SQLiteDatabase database = helper.getReadableDatabase();
        try {
            ContentValues contentValues = new ContentValues();
            contentValues.put(DbHelper.DbUser.userId, user.getId());
            contentValues.put(DbHelper.DbUser.name, user.getName());
            contentValues.put(DbHelper.DbUser.psw, user.getPsw());
            database.insert(DbHelper.DbUser.TBNAME, null, contentValues);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (database != null) {
                database.close();
            }
        }
//
//              StringBuffer sql = new StringBuffer()
//                .append("insert into ")
//                .append(DbHelper.DbUser.TBNAME)
//                .append("( ")
//                .append(DbHelper.DbUser.id).append(", ")
//                .append(DbHelper.DbUser.name).append(", ")
//                .append(DbHelper.DbUser.psw)
//
//                .append(" ) VAlUES (")
//                .append(user.getUserId()).append(", ")
//                .append(user.getName()).append(", ")
//                .append(user.getPsw())
//                .append(" )");
//
//        database.execSQL(sql.toString());


    }

    /**
     * @param user
     */
    @Override
    public void update(User user) {

    }

    @Override
    public void deleteById(int id) {

    }

    @Override
    public User find(String name) {
        return null;
    }

    @Override
    public List<User> findAll(int page) {
        int pageSize = 10;
        ArrayList<User> list = new ArrayList<>();
        SQLiteDatabase database = helper.getReadableDatabase();
        Cursor cursor = null;
        int start = page > 1 ? page * pageSize : 0;
        int end = page > 1 ? (page + 1) * pageSize : pageSize;
        StringBuffer sql = new StringBuffer()
                .append("select * from ")
                .append(DbHelper.DbUser.TBNAME)
                .append(" limit ")
                .append(start)
                .append(",")
                .append(end);
        try {
            cursor = database.rawQuery(sql.toString(), null);
            for (cursor.moveToFirst(); cursor.moveToLast(); cursor.moveToNext()) {
                User user = new User(cursor);
                list.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (database != null) {
                database.close();
            }
        }
        return list;
    }

    @Override
    public List<User> findAll() {
        ArrayList<User> list = new ArrayList<>();
        SQLiteDatabase database = helper.getReadableDatabase();
        Cursor cursor = null;

        StringBuffer sql = new StringBuffer()
                .append("select * from ")
                .append(DbHelper.DbUser.TBNAME);
        try {
            cursor = database.rawQuery(sql.toString(), null);
            for (cursor.moveToFirst(); cursor.moveToLast(); cursor.moveToNext()) {
                User user = new User(cursor);
                list.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (database != null) {
                database.close();
            }


        }


        return list;
    }


}
